package com.aspire.moss.portal.desktop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;

import com.aspire.moss.portal.R;

public class Main extends Activity {

    private static final String TAG = "Main";

    private Context mContext;

    private Workspace workspace;

    ClipImageView clipImageView;

    EventCenter eventCenter;

    DragLayer dragLayer;

    WorkspaceAdapter adapter;

    // DataAdapter bottomDataAdapter;

    // Dock dock;

    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "Main : onCreate");
        mContext = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d(TAG, "density :" + mContext.getResources().getDisplayMetrics().densityDpi);
        dragLayer = (DragLayer) findViewById(R.id.draglayer);
        workspace = (Workspace) findViewById(R.id.workspace);
        clipImageView = (ClipImageView) findViewById(R.id.clipview);
        // dock = (Dock) findViewById(R.id.bottomdesktop);
        workspace.setWorkspaceListener(clipImageView);
        eventCenter = new EventCenter(mContext, workspace, dragLayer);
        dragLayer.setInterceptTouchEvent(eventCenter);
        HashMap<Integer, List<AppInfo>> apps = createDatasourre();
        adapter = new WorkspaceAdapter(mContext, apps);
        adapter.setOnClickListener(eventCenter);
        adapter.setOnLongClickListener(eventCenter);
        adapter.setOnDeleteClickListener(eventCenter);

        // bottomDataAdapter = new DataAdapter(mContext, createBottomApp());
        // bottomDataAdapter.setOnClickListener(eventCenter);
        // bottomDataAdapter.setOnDeleteClickListener(eventCenter);
        // bottomDataAdapter.setOnLongClickListener(eventCenter);
        // dock.setAdapter(bottomDataAdapter);
        // bottomDataAdapter.notifyDataSetChanged();

        workspace.setWorkspaceAdapter(adapter);
        adapter.notifyObservers();

        // getSharedPreferences(name, mode)
    }

    private HashMap<Integer, List<AppInfo>> createDatasourre() {

        HashMap<Integer, List<AppInfo>> apps = new HashMap<Integer, List<AppInfo>>();
        List<AppInfo> page = null;
        AppInfo info = null;
        for (int i = 0; i < 4; i++) {
            page = new ArrayList<AppInfo>();
            for (int j = 1; j < 17; j++) {
                info = new AppInfo("1", "程序" + i + j, Utils.randomIcon(), 0, j);
                page.add(info);
            }
            apps.put(i, page);
        }
        return apps;
    }

    // public LinkedList<AppInfo> createBottomApp(){
    // LinkedList<AppInfo> page = new LinkedList<AppInfo>();
    // Random random = new Random();
    // AppInfo info = null;
    // for(int i = 0 ;i < 4 ;i++){
    // info = new AppInfo(i, 0, 0, getBitmap() , "程序程序" + random.nextInt(10),
    // "程序程序" + random.nextInt(10));
    // page.add(info);
    // }
    // return page;
    // }

    // private Bitmap excuteBitmap(Drawable drawable) {
    // Drawable temp = drawable;
    // while(temp==null){
    // temp = getBitmap();
    // }
    // BitmapDrawable bitmapDrawable =(BitmapDrawable) temp;
    // Bitmap bitmap = bitmapDrawable.getBitmap();
    //
    // int icon = (int) mContext.getResources().getDimension(R.dimen.icon);
    // Bitmap app = Bitmap.createBitmap(icon, icon, Config.ARGB_8888);
    //
    // android.graphics.Matrix matrix = new Matrix();
    // matrix.preScale(icon, icon);
    // Canvas canvas = new Canvas(app);
    // RectF rect = new RectF(0, 0, icon, icon);
    // Paint paint = new Paint();
    // paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    // canvas.drawRect(rect , paint );
    // canvas.drawBitmap(bitmap, matrix, null);
    // canvas.save();
    // return app;
    // }

    @Override
    public void onBackPressed() {
        if (workspace.isLockWorkspace()) {
            workspace.unlockWorkspace();
        } else {
            super.onBackPressed();
            Process.killProcess(Process.myPid());
        }

        adapter.printInfo();
    }

}
